package com.caiwm.service.impls;

import com.caiwm.common.ServerResponse;
import com.caiwm.common.SqlSessionUtils;
import com.caiwm.dao.StudentMapper;
import com.caiwm.entity.Student;
import com.caiwm.service.IStudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * <pre>
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃  神兽保佑
 * 　　　　┃　　　┃  代码无bug
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 * </pre>
 *
 * @author shiqla
 * @Desc 学生业务服务层
 * @Date 2019/1/7/007
 */
public class StudentService {



    public StudentService() {
    }

    public ServerResponse selectByCondition(Integer pageNum, Integer pageSize, Integer id, String name, String grade, String studentClass, Date admissionDate) {

        SqlSession sqlSession = SqlSessionUtils.getInstance().getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        PageHelper.startPage(pageNum, pageSize);
        List resultList = studentMapper.selectByCondition(id, name, grade, studentClass, admissionDate);
        PageInfo pageInfo = new PageInfo(resultList, 6);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse updateBatch(List<Student> list) {
        SqlSession sqlSession = SqlSessionUtils.getInstance().getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        try {
            for (Student student : list) {

                if(student.getId() == 0){
                    //如果没有ID,则直接插入
                    studentMapper.insertSelective(student);
                }else{
                    //如果有ID需要判断是否已经录入,如果没有录入则插入,如果已经录入则修改
                    Student student1 = studentMapper.selectByPrimaryKey(student.getId());
                    if(student1 == null){
                        studentMapper.insertSelective(student);
                    }else {
                        int result = studentMapper.updateByPrimaryKeySelective(student);
                        if (result <= 0) {
                            return ServerResponse.createByErrorMessage("更新学号为: " + student.getId() + " 学生信息失败!");
                        }
                    }
                }
            }
            return ServerResponse.createBySuccessMessage("批量更新成功");
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage("批量更新失败," + e.getMessage());
        }finally {
            sqlSession.commit();
        }
    }

    public ServerResponse deleteById(List<Integer> list){
        SqlSession sqlSession = SqlSessionUtils.getInstance().getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        for (Integer integer : list) {
            int result = studentMapper.deleteByPrimaryKey(integer);
            sqlSession.commit();
            if(result <= 0){
                return   ServerResponse.createByErrorMessage("删除学号为: " + integer + " 的学生信息失败!");
            }
        }
        return ServerResponse.createBySuccessMessage("删除成功");
    }

}
